package yuejian_07.yuejian_07.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.RetrofitListener;
import yuejian_07.yuejian_07.adapter.CommentListAdapter;
import yuejian_07.yuejian_07.base.RetrofitListenerTow;
import yuejian_07.yuejian_07.javabean.Comment;
import yuejian_07.yuejian_07.javabean.Show;
import yuejian_07.yuejian_07.model.CommentModel;
import yuejian_07.yuejian_07.model.ShowModel;

import static yuejian_07.yuejian_07.base.Constant.PHOTO_URL;

/**
 * Created by dell on 2017/10/16.
 */

public class ShowInfoActivity extends Activity implements RetrofitListener<Show>,RetrofitListenerTow<List<Comment>> {
    TextView u_name, s_time, s_content;
    ImageView img_return, u_pic, s_pic;

    int show_id;
    int user_id;
    String user_name;
    String show_time;
    String show_address;
    String show_content;
    String user_pic;
    String show_pic;

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_show_info);
        initViews();
        initEvents();
        initData();
    }

    public void initViews() {
        u_name = (TextView) findViewById(R.id.user_name);

        s_time = (TextView) findViewById(R.id.time);
        s_content = (TextView) findViewById(R.id.content);
        img_return = (ImageView) findViewById(R.id.img_return);
        u_pic = (ImageView) findViewById(R.id.user_pic);
        s_pic = (ImageView) findViewById(R.id.show_pic);
        recyclerView = (RecyclerView) findViewById(R.id.rv_commentlist);

    }

    public void initEvents() {
        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        u_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ShowInfoActivity.this, UserInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("user_id", user_id);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

    }

    public void initData() {

        getShowInfo();
        getCommentList();
    }

    //获取晒图信息数据
    public void getShowInfo() {

        Bundle bundle = getIntent().getExtras();
        show_id = bundle.getInt("s_id");
        user_id = bundle.getInt("user_id");
        ShowModel showModel = new ShowModel();
        showModel.getShowById(show_id, this);

    }

    //获取评论列表数据
    public void getCommentList() {
        CommentModel commentModel = new CommentModel();
        commentModel.getComments(show_id, this);
    }

    @Override
    public void onSuccess(Show show) {
        user_name = show.getUser_name();
        user_pic = PHOTO_URL + show.getUser_pic();
        show_address = show.getS_address();
        show_pic = PHOTO_URL + show.getS_pic();
        show_content = show.getS_content();
        show_time = show.getS_date();
        u_name.setText(user_name);
        s_content.setText(show_content);

        s_time.setText(show_time + "");
        //t_ask.setText(train_phone+"");
        if (show_pic != "") {
            Picasso.with(ShowInfoActivity.this)
                    .load(show_pic)
                    .into(s_pic);
            s_pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            s_pic.setImageResource(R.drawable.no_pic);
        }
        if (user_pic != "") {
            Picasso.with(ShowInfoActivity.this)
                    .load(user_pic)
                    .into(u_pic);
            u_pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            u_pic.setImageResource(R.drawable.no_pic);
        }

    }

    @Override
    public void onFail() {
        Toast.makeText(ShowInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSuccessTow(List<Comment> data) {

        //System.out.printf("data");
        recyclerView.setAdapter(new CommentListAdapter(ShowInfoActivity.this, data));
    }

    @Override
    public void onFailTow() {
        Toast.makeText(ShowInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();
    }

}
